extends Node3D

const SPEED = 20.0

var damage = 0
var angle = 0
var enemy
@onready var mesh = $MeshInstance3D
@onready var ray_cast_front = $RayCastFront
@onready var ray_cast_down = $RayCastDown
@onready var ray_cast_top = $RayCastTop
@onready var ray_cast_right = $RayCastRight
@onready var ray_cast_left = $RayCastLeft

func _process(delta):
	position += transform.basis * Vector3(0, angle, -SPEED) * delta
	if ray_cast_front.is_colliding():
		queue_free()
		if ray_cast_front.get_collider().has_method("receive_damage"):
			enemy = ray_cast_front.get_collider()
		elif ray_cast_front.get_collider().get_parent().has_method("receive_damage"):
			enemy = ray_cast_front.get_collider().get_parent()

	if ray_cast_down.is_colliding():
		if (ray_cast_down.get_collider().get_class() == "GridMap"):
			pass
		if (ray_cast_down.get_collider().get_class() != "GridMap"):
			mesh.visible = false
			queue_free()
			if ray_cast_down.get_collider().has_method("receive_damage"):
				enemy = ray_cast_down.get_collider()
			elif ray_cast_down.get_collider().get_parent().has_method("receive_damage"):
				enemy = ray_cast_down.get_collider().get_parent()

	if ray_cast_top.is_colliding():
		if (ray_cast_top.get_collider().get_class() == "GridMap"):
			pass
		if (ray_cast_top.get_collider().get_class() != "GridMap"):
			mesh.visible = false
			queue_free()
			if ray_cast_top.get_collider().has_method("receive_damage"):
				enemy = ray_cast_top.get_collider()
			elif ray_cast_top.get_collider().get_parent().has_method("receive_damage"):
				enemy = ray_cast_top.get_collider().get_parent()

	if ray_cast_right.is_colliding():
		if (ray_cast_right.get_collider().get_class() == "GridMap"):
			pass
		if (ray_cast_right.get_collider().get_class() != "GridMap"):
			mesh.visible = false
			queue_free()
			if ray_cast_right.get_collider().has_method("receive_damage"):
				enemy = ray_cast_right.get_collider()
			elif ray_cast_right.get_collider().get_parent().has_method("receive_damage"):
				enemy = ray_cast_right.get_collider().get_parent()

	if ray_cast_left.is_colliding():
		if (ray_cast_left.get_collider().get_class() == "GridMap"):
			pass
		if (ray_cast_left.get_collider().get_class() != "GridMap"):
			mesh.visible = false
			queue_free()
			if ray_cast_left.get_collider().has_method("receive_damage"):
				enemy = ray_cast_left.get_collider()
			elif ray_cast_left.get_collider().get_parent().has_method("receive_damage"):
				enemy = ray_cast_left.get_collider().get_parent()

	if enemy:
		enemy.receive_damage.rpc_id(enemy.get_multiplayer_authority(), damage)
		#enemy.apply_force_to_body.rpc_id(enemy.get_multiplayer_authority(), enemy.position)
